// HSS.Net.Mail.RecipientCommand.cs
// ----------------------------------------------------------------------------
// Licensed under the MIT license
// http://www.opensource.org/licenses/mit-license.html
// ----------------------------------------------------------------------------
// HighSpeed-Solutions, LLC
// Copyright (c) 2001-2010
// ----------------------------------------------------------------------------
// File:       RecipientCommand.cs
// Author:     HSS\gbanta
// Created:    08/12/2010
// Modified:   12/04/2010
// ----------------------------------------------------------------------------
namespace HSS.Net.Mail
{
	#region Using Directives
	using System;
	using System.Collections.Generic;
	using System.Text;
	using System.Net.Mail;
	#endregion

	#region RecipientCommand
	/// <summary>
	/// Used to send the list of recipients to an smtp server
	/// </summary>
	static class RecipientCommand
	{

		internal static bool Send(SmtpConnection smtpConn, System.Net.Mail.MailAddress recipient, out SmtpStatusCode serverResponseCode, out string serverResponse)
		{

			serverResponseCode = SmtpStatusCode.GeneralFailure;
			serverResponse = string.Empty;

			RecipientCommand.PrepareCommand(smtpConn, recipient);

			smtpConn.Send();

			smtpConn.GetResponse(out serverResponseCode, out serverResponse);

			return RecipientCommand.CheckResponse(serverResponseCode);

		}
		private static void PrepareCommand(SmtpConnection smtpConn, System.Net.Mail.MailAddress recipient)
		{
			string toAddress = string.Empty;

			if (!recipient.Address.StartsWith("<"))
				toAddress = "<" + recipient.Address;
			else
				toAddress = recipient.Address;

			if (!recipient.Address.EndsWith(">"))
				toAddress = toAddress + ">";

			smtpConn.Buffer.Append(SmtpCommands.Recipient);
			smtpConn.Buffer.Append(toAddress);
			smtpConn.Buffer.Append(SmtpCommands.CRLF);
		}
		private static bool CheckResponse(SmtpStatusCode code)
		{
			return (code == SmtpStatusCode.Ok);
		}
	}
	#endregion
}